package org.onejava.core.tools;

import java.io.File;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import org.onejava.core.exception.MMException;

/**
 * @author frostdb
 *
 *	扫描器工具类,用于自动扫描某包下的类
 */
public class ScanTool {
	public List<String> getAllClassName(String truePath,String path) throws Exception{
		return getClassesName(truePath,path,true,true);
	}
	public String getTruePath(){
		ClassLoader loader = Thread.currentThread().getContextClassLoader();
	    URL url = loader.getResource("");
		return url.getPath();
	}
	@SuppressWarnings("unused")
	public  List<String> getClassesName(String truePath,String path,Boolean childPackage,Boolean findAll) throws Exception{
		List<String> classNames = new ArrayList<String>();
		if(path==null||"".equals(path)){
			return classNames;
		}
		String packagePath = path.replace(".", "/");  
	    
	    File file = new File(truePath+packagePath);
		if(file.exists()){
			File[] childFiles = file.listFiles();
			for(File childFile:childFiles){
				if(childFile.isDirectory()&&childPackage){
					List list = this.getClassesName(truePath,packagePath+"/"+childFile.getName(),findAll,findAll);
					classNames.addAll(list);
				}else {
					String classname = childFile.getName();
					if(classname.endsWith(".class")){
						String name[] =classname.split("\\.");
						String classPath = path+"/"+name[0];
						if("package-info".equals(name[0])){
							continue;
						}
						classPath = classPath.replace("/", ".");
						classNames.add(classPath);
					}	
				}
			}
		}else{
			//throw new Exception("路径"+path+"无效!!!");
		}
		return classNames;
	}
}
